<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

// 本着严谨的原则，5.0版本默认情况下会对任何错误（包括警告错误）抛出异常，如果不希望如此严谨的抛出异常，可以在应用公共函数文件中或者配置文件中使用error_reporting方法设置错误报错级别
// 异常错误报错级别,（请注意，在入口文件中设置是无效的）
error_reporting(E_ALL & ~E_NOTICE);
define('NOW_TIME', $_SERVER['REQUEST_TIME']);
/**
 * 模拟tab产生空格
 * @param int $step
 * @param string $string
 * @param int $size
 * @return string
 */
function tab($step = 1, $string = ' ', $size = 4)
{
    return str_repeat($string, $size * $step);
}
function logs($name, $data) {

    file_put_contents(ROOT_PATH . 'log/' . $name . "_log.txt", var_export($data, TRUE), FILE_APPEND);
    file_put_contents(ROOT_PATH . 'log/' . $name . "2_log.txt", print_r($data, TRUE), FILE_APPEND);
    file_put_contents(ROOT_PATH . 'log/' . $name . "_log.txt", "\r\n", FILE_APPEND);
    file_put_contents(ROOT_PATH . 'log/' . $name . "2_log.txt", "\r\n", FILE_APPEND);
}
function send_stomp($topic, $data, $stomp_service = 'tcp://127.0.0.1:61613') {
    /**
      $stomp = new \Stomp('tcp://127.0.0.1:61613');
      $obj = new \Stdclass();
      //下面这些数据，实际中是用户通过前端页面post来的，这里只做演示
      $obj->url = Url::build('admin/student_statistics_api/statistics',null,null,true);
      $obj->user_id = $user_id;
      $obj->date = $date;
      $obj->key = $this->job_key;
      //发送一个注册消息到队列
      $stomp->send($this->job_key, json_encode($obj), array('persistent'=>"true"));
     * */
    $stomp =   new \FuseSource\Stomp\Stomp($stomp_service);
    $stomp->connect();
//    $obj = (object) $data;
    $re=$stomp->send($topic, json_encode($data), array('persistent' => true),TRUE);
    echo $re;
    return $re;
}

/**
 * 获取当前页面完整URL地址
 */
function get_url() {
    $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
    $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
    $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
    $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self . (isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : $path_info);
    return $sys_protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . $relate_url;
}

/**
 * 获取微信操作对象
 * @staticvar array $wechat
 * @param type $type
 * @return WechatReceive
 */
function &load_wechat( $get_type = null ,$type = 'Oauth') {
    !class_exists('Wechat\Loader', FALSE) && Vendor('Wechat.Loader');
    static $wechat = array();
    $index = md5(strtolower($type));
    if (!isset($wechat[$index])) {

        $model = Db('WechatConfig');
        $map = [];
        if ($get_type) {
            $map['config_key'] =$get_type;
            $model->where($map);
        }
        // 从数据库查询配置参数
        $config = $model->field('token,appid,appsecret,encodingaeskey,mch_id,partnerkey,ssl_cer,ssl_key,qrc_img')->find();
        // 设置SDK的缓存路径
        $config['cachepath'] = CACHE_PATH . 'Data/';
        $wechat[$index] = \Wechat\Loader::get_instance($type, $config);
    }
    return $wechat[$index];
}
function get_wx_config($get_type = "base") {
    $model = Db('WechatConfig');
    $map = [];
if ($get_type) {
    $map['config_key'] = config("weixin.$get_type");
    $model->where($map);
} else if (config("weixin.base")) {
    $map['config_key'] = config("weixin.base");
    $model->where($map);
}
// 从数据库查询配置参数
$config = $model->find();
logs('get_wx_config',$model->getLastSql());
logs('get_wx_config',$config);
return $config;
}
